<?php

namespace app\logic\single;

use app\logic\model\user\UserMusicLogicModel;

class UserMusicSingle
{


    private static $inc;


    private function __clone(){

    }

    public static function getInstance(){
        if(is_null(self::$inc)){
            self::$inc = new self();
        }
        return self::$inc;
    }
    private function __construct()
    {
        $this->userMusicModel = new UserMusicLogicModel();
    }

    function save($user_id,$music_id,$source=''){
        $check = $this->isHave($user_id,$music_id);
        if(!$check){
            $this->userMusicModel->save([
                'user_id'=>$user_id,
                'music_id'=>$music_id,
                'source'=>$source,
                'create_time'=>date('Y-m-d H:i:s')
            ]);
        }
    }
    public function saves(array $user_music_arr)
    {
        return $this->userMusicModel->saveAll($user_music_arr);
    }
    function isHave($music_id,$user_id): bool
    {
        if(empty($music_id) || empty($user_id)){
            return false;
        }
        return (bool)$this->userMusicModel->where('music_id', $music_id)
            ->where('user_id', $user_id)
            ->find();
    }
}